package com.atguigu.eduservice.service.impl;

import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.vo.ChapterVo;
import com.atguigu.eduservice.vo.VideoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dayouzi
 * @since 2022-10-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    /**
     * 一个课程id对应多个章节，一个章节对应多个视频
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> nestedList(String courseId) {
        //结果列表
        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();

        //获取传入课程id对应的所有章节信息
        QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        queryWrapper1.orderByAsc("sort", "id");
        List<EduChapter> chapters = baseMapper.selectList(queryWrapper1);

        //获取传入课程id对应的所有课程视频信息
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        queryWrapper2.orderByAsc("sort", "id");
        List<EduVideo> videos = videoService.list(queryWrapper2);

        //遍历所有章节
        for (EduChapter chapter : chapters) {
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            //遍历视频，取出对应当前章节的视频
            for (EduVideo video : videos) {
                if (video.getChapterId().equals(chapter.getId())) {
                    //填充
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            //填充
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVo.setChildren(videoVoArrayList);

            chapterVoArrayList.add(chapterVo);
        }
        return chapterVoArrayList;
    }

    @Override
    public boolean removeChapterById(String id) {
        //根据id查询是否存在视频，如果有则提示用户尚有子节点
        if (videoService.getCountByChapterId(id)) {
            throw new GuliException(ResultCodeEnum.REMOVECHA_ERROR);
        }

        Integer result = baseMapper.deleteById(id);

        return result > 0;
    }

    @Override
    public boolean removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        Integer result = baseMapper.delete(queryWrapper);
        return result > 0;
    }
}
